/**
 * 项目名称:
 *
 * @Author: 夏国倩
 * @Date: 2023/10/16 16:51
 * @Copyright
 */
package JDBC;
import java.sql.*;

/*
PreparedStatement 完成insert delete update
 */
public class JDBCtest9 {
    public static void main(String[] args) {
        Connection conn=null;
        PreparedStatement ps=null;
        try{
            try {
                //1.注册驱动
                Class.forName("com.mysql.cj.jdbc.Driver");
                //2.获取连接
                conn = DriverManager.getConnection(
                        "jdbc:mysql://localhost:3306/base4?useSSL=false&allowPublicKeyRetrieval=true&serverTimezone=UTC",
                        "root",
                        "123456");
                //3.获取预编译的数据库操作对象
                /*String sql="insert into emp(empno,ename,job,mgr,hiredate,sal,comm,deptno) values (?,?,?,?,?,?,?,?)";
                ps=conn.prepareStatement(sql);
                ps.setInt(1,1101);
                ps.setString(2,"小乔");
                ps.setString(3,"SALEMAN");
                ps.setInt(4,2);
                ps.setDate(5,new Date(System.currentTimeMillis()));
                ps.setDouble(6,9000.0);
                ps.setDouble(7,890);
                ps.setInt(8,22);*/

                /*String sql="update emp set ename =?,sal=? where empno=?";
                ps=conn.prepareStatement(sql);
                ps.setString(1,"张三");
                ps.setDouble(2,8888.0);
                ps.setInt(3,1101);*/

                String sql="delete from emp where empno=?";
                ps=conn.prepareStatement(sql);
                ps.setInt(1,1101);

                //4.执行SQL
                int count=ps.executeUpdate();
                System.out.println(count);
            } catch (ClassNotFoundException e) {
                throw new RuntimeException(e);
            }
        }catch (Exception e){
            throw new RuntimeException(e);
        }finally {
            if (ps != null) {
                try {
                    ps.close();
                } catch (SQLException e) {
                    throw new RuntimeException(e);
                }
            }
            if (conn != null) {
                try {
                    conn.close();
                } catch (SQLException e) {
                    throw new RuntimeException(e);
                }
            }
        }
    }
}
